package ui.activity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.HousesListBean;
import com.fy.fyzf.bean.ModifySignBean;
import com.fy.fyzf.bean.PassengerNumberBean;
import com.fy.fyzf.bean.SignDetailBean;
import com.fy.fyzf.dialog.LoopViewPopwindow;
import com.fy.fyzf.presenter.AddSignPresenter;
import com.fy.fyzf.utils.DateUtils;
import com.fy.fyzf.utils.SPUtils;
import com.fy.fyzf.view.AddSignCallback;
import com.google.gson.Gson;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ui.adapter.CheckBoxAdapter;
import ui.adapter.FenPeiInfoAdapter;

public class SignContractDetailActivity extends BaseActivity<AddSignPresenter> implements AddSignCallback {

    @BindView(R.id.rb1)
    RadioButton rb1;
    @BindView(R.id.rb2)
    RadioButton rb2;
    @BindView(R.id.rb3)
    RadioButton rb3;
    @BindView(R.id.rg)
    RadioGroup rg;
    @BindView(R.id.tv_date)
    TextView tvDateClose;
    @BindView(R.id.tv_number_passenger)
    TextView tvNumberPassenger;
    @BindView(R.id.tv_number_housing)
    TextView tvNumberHousing;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.et_counselor)
    EditText etCounselor;
    @BindView(R.id.tv_conpany)
    TextView tvConpany;
    @BindView(R.id.tv_staff_name)
    TextView tvStaffName;
    @BindView(R.id.recycleView1)
    RecyclerView recycleView1;
    @BindView(R.id.recycleView2)
    RecyclerView recycleView2;
    @BindView(R.id.et_area)
    EditText etArea;
    @BindView(R.id.tv_sign_date)
    TextView tvSignDate;
    @BindView(R.id.tv_pay_type)
    EditText tvPayType;
    @BindView(R.id.tv_jiaofang_time)
    TextView tvJiaofangTime;
    @BindView(R.id.tv_rent)
    EditText tvRent;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.et_owner_name)
    EditText etOwnerName;
    @BindView(R.id.et_contacts)
    EditText etContacts;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_contacts1)
    EditText etContactsHz;
    @BindView(R.id.et_count)
    EditText etCount;
    @BindView(R.id.et_phone_step2)
    EditText etPhoneStep2;
    @BindView(R.id.recycleView_source)
    RecyclerView recycleViewSource;
    @BindView(R.id.et_owner_comm)
    EditText etOwnerComm;
    @BindView(R.id.tv_payment_time)
    TextView tvPaymentTime;
    @BindView(R.id.et_selef_performance)
    EditText etSelefPerformance;
    @BindView(R.id.et_rebate)
    EditText etRebate;
    @BindView(R.id.recycleView_type)
    RecyclerView recycleViewType;
    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.recycleView3)
    RecyclerView recycleView3;
    @BindView(R.id.tv_next)
    TextView tvNext;
    private FenPeiInfoAdapter fenPeiInfoAdapter;
    private CheckBoxAdapter checkBoxAdapter1;
    private CheckBoxAdapter checkBoxAdapter2;
    private CheckBoxAdapter checkBoxAdapter;
    private int customersId;
    private int performanceId;

    @Override
    protected AddSignPresenter createPresenter() {
        return new AddSignPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_sign_contract_detail;
    }

    @Override
    public void initView() {
        super.initView();
        fenPeiInfoAdapter = new FenPeiInfoAdapter(2);
        recycleView3.setLayoutManager(new LinearLayoutManager(this));
        recycleView3.setAdapter(fenPeiInfoAdapter);
        setTitleName("签约详情");
        initOne();
        initTwo();
    }

    private ModifySignBean modifySignBean = new ModifySignBean();

    private void initTwo() {
        List<String> titles = new ArrayList<>();
        titles.add("老顾客及朋友介绍");
        titles.add("网络");
        titles.add("上门");
        titles.add("其他");
        checkBoxAdapter = new CheckBoxAdapter(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        recycleView.setLayoutManager(gridLayoutManager);
        recycleView.setAdapter(checkBoxAdapter);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 3) {
                    return 3;
                }
                return 1;
            }
        });
        checkBoxAdapter.setNewData(titles);

//        List<String> titles3 = new ArrayList<>();
//        titles3.add("工行");
//        titles3.add("中行");
//        titles3.add("支付宝");
//        titles3.add("微信");
//        titles3.add("其他");

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 4) {
                    return 3;
                }
                return 1;
            }
        });
//        checkBoxAdapter3.setNewData(titles3);
    }

    private void initOne() {
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb1) {
                    modifySignBean.setClinchDealType(1);
                } else if (checkedId == R.id.rb2) {
                    modifySignBean.setClinchDealType(2);
                } else {
                    modifySignBean.setClinchDealType(3);
                }
            }
        });

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        checkBoxAdapter1 = new CheckBoxAdapter(this);

        recycleView1.setLayoutManager(gridLayoutManager);
        recycleView1.setAdapter(checkBoxAdapter1);

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 5) {
                    return 3;
                }
                return 1;
            }
        });
        List<String> list1 = new ArrayList<>(); //性质 propertyNature
        list1.add("公寓");
        list1.add("写字楼");
        list1.add("商铺");
        list1.add("工位");
        list1.add("花园");
        list1.add("其他");
        checkBoxAdapter1.setNewData(list1);

        checkBoxAdapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                checkBoxAdapter1.setSelect(position);
//                addSignOne.setPropertyNature(String.valueOf(position + 1));
            }
        });

        checkBoxAdapter2 = new CheckBoxAdapter(this);
        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(this, 3);

        recycleView2.setLayoutManager(gridLayoutManager1);
        recycleView2.setAdapter(checkBoxAdapter2);

        gridLayoutManager1.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 5) {
                    return 3;
                }
                return 1;
            }
        });


        List<String> list2 = new ArrayList<>(); //装修情况 propertyDecoration
        list2.add("全配");
        list2.add("简配");
        list2.add("豪华装修");
        list2.add("简单装修");
        list2.add("毛坯");
        list2.add("其他");
        checkBoxAdapter2.setNewData(list2);

        checkBoxAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                checkBoxAdapter2.setSelect(position);
//                addSignOne.setPropertyDecoration(String.valueOf(position + 1));
            }
        });


        tvConpany.setText(SPUtils.getInstance(this).getString("company", ""));
        tvStaffName.setText(SPUtils.getInstance(this).getString("name", ""));
    }

    @Override
    public void initData() {
        super.initData();
        performanceId = getIntent().getIntExtra("performanceId", 0);
        customersId = getIntent().getIntExtra("customersId", 0);
        mPresenter.getSingnContractdetail(performanceId);
    }

    @Override
    public void getPassengerNumberListSuccess(List<PassengerNumberBean> list) {
        List<String> titlts = new ArrayList<>();
        List<Integer> idS = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            titlts.add(list.get(i).getCustomerName() + "-" + list.get(i).getCustomerNumber());
            idS.add(list.get(i).getCustomerId());
        }
        LoopViewPopwindow loopViewPopwindow = new LoopViewPopwindow(this);
        loopViewPopwindow.setIds(idS).setTitleData(titlts).setTitle(" ").showPop(etArea);
        loopViewPopwindow.getView(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvNumberPassenger.setText(titlts.get(loopViewPopwindow.position));
                loopViewPopwindow.dismiss();
            }
        });

    }

    @Override
    public void saveOneStepSuccess(BaseData baseData) {

    }

    @Override
    public void saveTwoStepSuccess(BaseData baseData) {

    }

    @Override
    public void saveThreeStepSuccess(BaseData baseData) {

    }

    @Override
    public void getSingnContractdetail(SignDetailBean signContractDetailBean) {
        showData(signContractDetailBean);
    }

    @Override
    public void exChangeSignContractSuccess(BaseData data) {

    }

    private void showData(SignDetailBean data) {
        if (data != null) {
            switch (data.getClinchDealType()) {
                case 1:
                    rb1.setChecked(true);
                    break;
                case 2:
                    rb2.setChecked(true);
                    break;
                case 3:
                    rb3.setChecked(true);
                    break;
                default:
            }
            modifySignBean.setCustomersId(customersId);
            modifySignBean.setClinchDealType(data.getClinchDealType());
            checkBoxAdapter1.setSelect(Integer.parseInt(data.getPropertyNature()) - 1);
            modifySignBean.setPropertyNature(data.getPropertyNature());
            etArea.setText(data.getPropertyArea());
            modifySignBean.setPropertyArea(data.getPropertyArea());
            modifySignBean.setPerformanceId(performanceId);
            modifySignBean.setCommissionPayType(data.getCommissionPayType());

            if (data.getPropertyDecoration() == null) {
                checkBoxAdapter2.setSelect(0);
            } else {
                checkBoxAdapter2.setSelect(Integer.parseInt(data.getPropertyDecoration()) - 1);

            }
            modifySignBean.setPropertyDecoration(data.getPropertyDecoration());

            checkBoxAdapter.setSelect(data.getCustomerSource() - 1);  //来源;
            modifySignBean.setOwnerSource(String.valueOf(data.getCustomerSource()));
            modifySignBean.setCustomerSource(data.getCustomerSource());
            tvDateClose.setText(DateUtils.formatDate(data.getClosingDate()));
            modifySignBean.setClosingDate(data.getClosingDate());
            tvNumberPassenger.setText(data.getCustomerNumber());
            modifySignBean.setCustomerCheckNumber(data.getCustomerNumber());
            tvNumberHousing.setText(data.getHousingNo());
            modifySignBean.setHousingId(data.getHousingId());

            etCounselor.setText(data.getPropertyConsultant());
            modifySignBean.setPropertyConsultant(data.getPropertyConsultant());

            tvSignDate.setText(DateUtils.formatDate(data.getTradingSignDate()));
            modifySignBean.setTradingSignDate(data.getTradingSignDate());

            tvPayType.setText(data.getTradingPayment());
            modifySignBean.setTradingPaymentDate(data.getTradingPaymentDate());
            modifySignBean.setTradingPayment(data.getTradingPayment());
            tvJiaofangTime.setText(DateUtils.formatDate(data.getTradingPaymentDate()));
            modifySignBean.setTradingPaymentDate(data.getTradingPaymentDate());
            tvRent.setText(data.getTradingVolRent());
            modifySignBean.setTradingVolRent(data.getTradingVolRent());
            tvStartTime.setText(DateUtils.formatDate(data.getTradingBeginTime()));
            modifySignBean.setTradingBeginTime(data.getTradingBeginTime());
            tvEndTime.setText(DateUtils.formatDate(data.getTradingEndTime()));
            modifySignBean.setTradingEndTime(data.getTradingEndTime());
            etOwnerName.setText(data.getOwnerLandlordName());
            modifySignBean.setOwnerLandlordName(data.getOwnerLandlordName());
            etContactsHz.setText(data.getOwnerContact());
            modifySignBean.setOwnerContact(data.getOwnerContact());
            etPhoneStep2.setText(data.getOwnerPhone());
            modifySignBean.setOwnerPhone(data.getOwnerPhone());

            etName.setText(data.getCustomerName());
            modifySignBean.setCustomerName(data.getCustomerName());
            etContacts.setText(data.getCustomerContact());
            modifySignBean.setCustomerContact(data.getCustomerContact());
            etCount.setText(data.getCustomerCheckNumber());
            modifySignBean.setCustomerCheckNumber(data.getCustomerCheckNumber());
            etPhone.setText(data.getCustomerPhone());
            modifySignBean.setCustomerPhone(data.getCustomerPhone());
            etOwnerComm.setText(data.getCommissionOwner());
            modifySignBean.setCommissionOwner(data.getCommissionOwner());
            tvPaymentTime.setText(DateUtils.formatDate(data.getCommissionPayTime()));
            modifySignBean.setCommissionPayTime(data.getCommissionPayTime());
            etSelefPerformance.setText(data.getCommissionResults());
            modifySignBean.setCommissionResults(data.getCommissionResults());
            etRebate.setText(data.getCommissionHold());
            modifySignBean.setCommissionHold(data.getCommissionHold());

            List<ModifySignBean.PerformanceUserParamsBean> list = new ArrayList<>();
            for (int i = 0; i < data.getPerformanceUserDetailsVos().size(); i++) {
                SignDetailBean.PerformanceUserDetailsVosBean vosBean = data.getPerformanceUserDetailsVos().get(i);
                list.add(new ModifySignBean.PerformanceUserParamsBean(vosBean.getAssignGroupName(),vosBean.getAssignName(),vosBean.getAssignPct(),vosBean.getAssignReason(),vosBean.getAssignTurnover(),vosBean.getId()));
            }
             modifySignBean.setPerformanceUserParams(list);

            fenPeiInfoAdapter.setNewData(data.getPerformanceUserDetailsVos());
            Log.e("签约数据", new Gson().toJson(modifySignBean));
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @OnClick({R.id.tv_payment_time, R.id.tv_date, R.id.tv_number_passenger, R.id.tv_search, R.id.tv_sign_date, R.id.tv_jiaofang_time, R.id.tv_start_time, R.id.tv_end_time, R.id.tv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_date:
                showDateDialog(tvDateClose, 1);
                break;
            case R.id.tv_number_passenger:
                mPresenter.getPassengerNumber();
                break;
            case R.id.tv_search:
                Intent intent = new Intent(this, AllHousingActivity.class);
                intent.putExtra("type", 2);
                startActivityForResult(intent, 10001);
                break;
            case R.id.tv_sign_date:
                showDateDialog(tvSignDate, 2);
                break;
            case R.id.tv_jiaofang_time:
                showDateDialog(tvJiaofangTime, 3);
                break;
            case R.id.tv_start_time:
                showDateDialog(tvStartTime, 4);
                break;
            case R.id.tv_end_time:
                showDateDialog(tvEndTime, 5);
                break;
            case R.id.tv_payment_time:
                showDateDialog(tvPaymentTime, 6);
                break;
            case R.id.tv_next:
                mPresenter.exChangeSignContract(modifySignBean);
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void showDateDialog(TextView textView, int type) {
        //获取日历的一个实例，里面包含了当前的年月日
        Calendar calendar = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(SignContractDetailActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                int str;
                if (month == 0) {
                    str = 1;
                } else {
                    str = month + 1;
                }
                textView.setText(year + "-" + str + "-" + dayOfMonth);
                if (type == 1) {
                    modifySignBean.setClosingDate(textView.getText().toString() + " 00:00:00");
                } else if (type == 2) {
                    modifySignBean.setTradingSignDate(textView.getText().toString() + " 00:00:00");
                } else if (type == 3) {
                    modifySignBean.setTradingPaymentDate(textView.getText().toString() + " 00:00:00");
                } else if (type == 4) {
                    modifySignBean.setTradingBeginTime(textView.getText().toString() + " 00:00:00");
                } else if (type == 5) {
                    modifySignBean.setTradingEndTime(textView.getText().toString() + " 00:00:00");
                } else {
                    modifySignBean.setCommissionPayTime(textView.getText().toString() + " 00:00:00");

                }
            }
        },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 10001) {
            HousesListBean.ListBean listBean = (HousesListBean.ListBean) data.getSerializableExtra("data");
            tvNumberHousing.setText(listBean.getAreaName() + "-" + listBean.getHousingNo());
            modifySignBean.setCustomersId(customersId);
            modifySignBean.setHousingId(listBean.getHousingId());
        }
    }
}